package com.szy.douyou_article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szy.douyou_article.config.ResultDTO;
import com.szy.douyou_article.dao.Article;
import com.szy.douyou_article.dao.Comment;
import com.szy.douyou_article.dao.Users;
import com.szy.douyou_article.dao.mapper.CommentMapper;
import com.szy.douyou_article.dao.mapper.UsersMapper;
import com.szy.douyou_article.dao.req.CommentReq;
import com.szy.douyou_article.service.CommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sunziyan
 * @date 2022/4/11
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentMapper commentMapper;


    // 处理每个父级评论的子级及其嵌套子级
    public List<Comment> findParent(List<Comment> comments) {

        for (Comment comment : comments) {
            ArrayList<Comment> fatherChildren = new ArrayList<>();
            // 递归处理子级的回复，即回复内有回复
            findChildren(comment, fatherChildren);
            // 将递归处理后的集合放回父级的孩子中
            comment.setChildren(fatherChildren);
        }
        return comments;
    }
    public void findChildren(Comment parent, List<Comment> fatherChildren) {

        // 找出直接子级
        List<Comment> comments = commentMapper.getCommentByArticleId(parent.getArticleId(), parent.getCommentId());

        // 遍历直接子级的子级
        for (Comment comment : comments) {

            // 若非空，则还有子级，递归
            if (!commentMapper.getCommentByArticleId(parent.getArticleId(), parent.getCommentId()).isEmpty()) {
                findChildren(comment, fatherChildren);
            }

            // 已经到了最底层的嵌套关系，将该回复放入新建立的集合
            fatherChildren.add(comment);
            // 解除了嵌套关系
            comment.setChildren(new ArrayList<>());
        }
    }

    @Override
    public ResultDTO getCommentByArticleId(Long articleId) {

        List<Comment> commentList = commentMapper.getCommentByArticleId(articleId, -1L);
        findParent(commentList);
        //补全每个评论的父信息
        for (Comment comment : commentList) {
            for (Comment comment1 : comment.getChildren()) {
                if (comment1.getCommentFatherId().equals(comment.getCommentId())) {
                    continue;
                }
                Users fatherUser = commentMapper.getFatherCommentUserMsg(comment1.getCommentFatherId());
                comment1.setFatherName(fatherUser.getUserName());
                comment1.setFatherAvatar(fatherUser.getUserAvatar());
            }

        }

        return ResultDTO.success(commentList);
    }

    @Override
    public ResultDTO addOneComment(Comment comment) {
        int result = commentMapper.insert(comment);
        int result1 = commentMapper.updateArticleCommentNumbers(comment.getArticleId());
        if (result == 0 || result1 == 0) {
            return ResultDTO.fail("添加评论失败");
        }
        return ResultDTO.success(result);
    }

    @Override
    public ResultDTO getAllComment(CommentReq commentReq) {
        Page<Comment> commentPage = new Page<>(commentReq.getPageNo(), commentReq.getPageSize());
        commentPage = commentMapper.getAllComment(commentPage, commentReq);
        return ResultDTO.success(commentPage);
    }

    @Override
    public ResultDTO deleteCommentByCommentId(Long commentId) {
        Comment comment = commentMapper.selectById(commentId);
        if (comment == null){
            return ResultDTO.fail("找不到该评论");
        }
        Integer result = commentMapper.deleteById(commentId);
        //把所有子的父id换成自己的父id
        if (comment.getCommentFatherId() != -1) {
            result = commentMapper.updateCommentFatherIdWhenDelete(comment);

        }

        return ResultDTO.success(result);
    }


}
